#ifndef FILE_H
#define FILE_H

#include "Common.h"
#include "Host.h"

class File{
private:
    string _fileName;
    int _chunks;
    int _localChunks;
    bool * _chunkStatus;     // keeps track of which chunk is present locally
    int * _copiesOnSystem;   // keeps track how many of each chunk is present in system    

public:
    File(string fileName, int chunks);
    ~File();
    // accessors
    string fileName();  // returns name of the file
    int chunks();       // returns number of chunks this file has
    int localChunks();  // return number of chunks available locally
    int systemChunks(); // returns number of chunks available in the system
    int fileReplicationLevel();         // returns weighted file replication level of system. if equals to numConnected, no peer needs this       
    int minimumChunkReplicationLevel();   // returns min rep level of all chunks 
    int leastReplicatedChunk();     // returns the chunk(present locally) number that is has least replication level in the file
    int chunkReplicationLevel(int chunk);    // returns system replication level of a chunk. from chunk 1 to size
    bool isChunkPresent(int chunk);     // checks if a particular chunk is present locally. from chunk 1 to size
    // used to update
    int receivedChunk(int chunk);   // set local chunk to true; this chunk is present locally; increase copiesOnSystem
    int successTransfer(int chunk);     // increase copiesOnSystem, system now has one more copy of this chunk
    int decreaseSystemChunk(int chunk);     // decrease copiesOnSystem, system now has one less copy of this chunk
    int completeFile();         // have the complete file now.
    bool isComplete();          // does the file have all required chunks?
    int revertToLocalData();    // wipe out system data for this file, retain only local data as system data
    bool hasAnyChunk();         // returns true if part of the file is present locally, false if no part of the file is present locally.


    // new things
    File(string fileName, Host host, bool isLocalHosting, int versions);
    int _versions;
    bool _isFileAvailableOnNetwork;
    vector<bool> _isVersionPresentLocally;
    vector<int> _currentRevisions; // one revision per version
    enum UpdateStatus { upToDate, notUpToDate, unknown };
    vector<UpdateStatus> _needsUpdate;
    Host * _host;
    bool _modified;
    enum Status { opened, closed };
    vector<Status> _status;
    int receivedLocalVersion(int versionNum, int revisionNum);
    int setFileStatus(int versionNum, Status newStatus);
    int modifyFile(int versionNum);
    int saveFile(int versionNum, int revisionNum);
    int setVersionAvailability(bool availabilityOnNetwork);
    bool isAvailable(int VersionNum);

    // hosting information
    int _totalRevisions;
    bool _isLocalHosting;
    int removeHostingInformation();
    int addHostingInformation(int versions, vector<int> currentRevisions, int totalRevisions);
};

#endif
